---OOSE SoSe 2011 Aufgabe 3
--@author Mickey Reiss <mickeyreiss@gmail.com> (HU:417884)
--@author Oleg Mayevskiy <mayevskiy.oleg@googlemail.com> (233197)

require('classes')
require('Player')
require('logger')

Class{'Game',
  player1 = Player,
  player2 = Player,
  currentPlayer = Player,
  logger = Logger,
  finished = Boolean
}

function Game:restart()
	self:init()
end

---initialize games by creating players and logger
--most game implementations can simply call super:init() to simplify
--their initialization sequences
function Game:init()
	--set logging level
	self.logger = Logger:create()
	self.logger.level = self.logger.WARNING

	--set players
	self.player1 = Player:create()
	self.player1.nr = 1
	self.player2 = Player:create()
	self.player2.nr = 2
	self.currentPlayer = self.player1

	self.finished = false
end

--must return nextPlayer
function Game:doMove(nr)
	error('not yet implemented')
end

--must return winner, isGameFinished
function Game:checkVictory()
	error('not yet implemented')
end

--returns nextPlayerName, isGameFinished
function Game:move(nr)
	if(0 == nr) then
		print('you typed 0, game is now explicit finished and closed')
		os.exit()
	end


	if(not self:checkMoveInput(nr)) then
		return self.currentPlayer.name, false
	end

	if(not self:validateMove(nr)) then
		return self.currentPlayer.name, false
	end

	--we don`t want to switch player, if game is over!
	local nextPlayer = self:doMove(nr)

	local winner, isGameOver = self:checkVictory(nr)
  
	--switch player
	if(not isGameOver) then
		self.currentPlayer = nextPlayer
	--return winner name
	else
		--if draw then nil
		if(isNil(winner)) then
			return nil, isGameOver
		--else winner name
		else
			return winner.name, isGameOver
		end
	end

	return self.currentPlayer.name, isGameOver

end

function Game:print()
	error('not yet implemented')
end

function Game:printPossibleTurns()
	error('not yet implemented')
end

--validate if a move is valid
function Game:validateMove(nr)
  error('not yet implemented')
end

--check if input is valid
function Game:checkMoveInput(input)
  error('not yet implemented')
end

--returns non-current player
function Game:otherPlayer()
	if(self.player1==self.currentPlayer) then
		return self.player2
	else
		return self.player1
	end
end


